package com.ruoyi.lyy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.lyy.domain.pojo.Evaluate;
import com.ruoyi.lyy.domain.pojo.ServiceDetails;
import com.ruoyi.lyy.domain.pojo.User;
import com.ruoyi.lyy.domain.vo.EvaluationVO;
import com.ruoyi.lyy.domain.vo.PageQueryVO;
import com.ruoyi.lyy.domain.vo.ServiceDetailVO;
import com.ruoyi.lyy.mapper.EvaluateMapper;
import com.ruoyi.lyy.mapper.ServiceDetailsMapper;
import com.ruoyi.lyy.mapper.UserMapper;
import com.ruoyi.lyy.service.IServiceDetailsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务详情表 服务实现类
 * </p>
 *
 * @author YueYang
 * @since 2025-11-12
 */
@Service
@RequiredArgsConstructor
public class ServiceDetailsServiceImpl extends ServiceImpl<ServiceDetailsMapper, ServiceDetails> implements IServiceDetailsService {

    //注入评价表的mapper
    private final EvaluateMapper evaluateMapper;

    //注入服务详情表的mapper
    private final ServiceDetailsMapper serviceDetailsMapper;

    //注入用户表的mapper
    private final UserMapper userMapper;


    /**
     * 根据服务类型id 查询所有商户id
     *
     * @return 返回商户id集合
     */
    @Override
    public List<Integer> getMerchantIdsByTypeId(Integer typeId) {
        // 1. 构建查询条件
        LambdaQueryWrapper<ServiceDetails> lambdaQueryWrapper = new LambdaQueryWrapper<ServiceDetails>()
                .eq(ServiceDetails::getTypeId, typeId);

        // 2. 执行查询
        List<ServiceDetails> serviceDetailsList = serviceDetailsMapper.selectList(lambdaQueryWrapper);

        //3. 提取商户id列表，同时对id去重
        return serviceDetailsList.stream()
                .map(ServiceDetails::getMId)
                .distinct()
                .collect(Collectors.toList());
    }

    /**
     * 根据商户id 查询该商户对应的服务
     *
     * @param id 商户id
     * @return 返回服务详情集合
     */
    @Override
    public List<ServiceDetailVO> getServiceDetailsByMid(Integer id) {
        //1. 构建插叙条件
        LambdaQueryWrapper<ServiceDetails> lambdaQueryWrapper = new LambdaQueryWrapper<ServiceDetails>()
                .eq(ServiceDetails::getMId, id);
        //2. 执行查询
        List<ServiceDetails> serviceDetails = serviceDetailsMapper.selectList(lambdaQueryWrapper);

        //3.封装VO返回结果
        return serviceDetails.stream().map(serviceDetail -> {
            ServiceDetailVO serviceDetailVO = new ServiceDetailVO();
            serviceDetailVO.setId(serviceDetail.getId());
            serviceDetailVO.setMId(serviceDetail.getMId());
            serviceDetailVO.setName(serviceDetail.getName());
            serviceDetailVO.setIntroduction(serviceDetail.getIntroduction());
            serviceDetailVO.setOriginalPrice(serviceDetail.getOriginalPrice());
            serviceDetailVO.setPresentPrice(serviceDetail.getPresentPrice());
            return serviceDetailVO;
        }).collect(Collectors.toList());
    }

    /**
     * 根据商户id 查询该商户对应的评价
     *
     * @param mid      商户id
     * @param pageNum  页码
     * @param pageSize 每页大小
     * @return 返回评价集合
     */
    @Override
    public PageQueryVO<EvaluationVO> getEvaluateByMid(Integer mid, Integer pageNum, Integer pageSize) {
        //1. 开启分页插件
        if (pageNum == null || pageSize == null) {
            throw new RuntimeException("分页参数不能为空，请检查");
        } else {
            //开启分页插件
            PageHelper.startPage(pageNum, pageSize);
        }

        //2. 构建查询条件
        LambdaQueryWrapper<Evaluate> lambdaQueryWrapper = new LambdaQueryWrapper<Evaluate>()
                .eq(Evaluate::getMerchantId, mid);
        //3. 执行查询
        List<Evaluate> evaluateList = evaluateMapper.selectList(lambdaQueryWrapper);

        //4. 创建PageInfo对象
        PageInfo<Evaluate> pageInfo = new PageInfo<>(evaluateList);

        //5. 封装返回结果
        List<EvaluationVO> evaluationVOS = pageInfo.getList().stream().map(evaluate -> {
            EvaluationVO evaluationVO = new EvaluationVO();
            evaluationVO.setId(evaluate.getId());
            evaluationVO.setMerchantId(evaluate.getMerchantId());
            evaluationVO.setOrderId(evaluate.getOrderId());
            evaluationVO.setRate(evaluate.getRate());
            evaluationVO.setImgs(evaluate.getImgs());
            evaluationVO.setContent(evaluate.getContent());
            evaluationVO.setTime(evaluate.getTime());
            //5.1 查询用户表 获取用户的名字
            //5.1.1 构建查询条件
            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<User>()
                    .eq(User::getId, evaluate.getUserId());
            //5.1.2 执行查询
            User user = userMapper.selectOne(userLambdaQueryWrapper);
            //5.1.3 封装结果
            evaluationVO.setUserName(user.getNickName());

            return evaluationVO;
        }).collect(Collectors.toList());

        //6. 封装PageQueryVO对象
        PageQueryVO<EvaluationVO> pageQueryVO = new PageQueryVO<>();
        pageQueryVO.setPageNum(pageInfo.getPageNum());
        pageQueryVO.setTotal(pageInfo.getTotal());
        pageQueryVO.setPages(pageInfo.getPages());
        pageQueryVO.setList(evaluationVOS);
        return pageQueryVO;
    }
}
